package com.growthsee.rider.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.growthsee.common.constant.Constants;
import com.growthsee.common.core.domain.AjaxResult;
import com.growthsee.common.core.redis.RedisCache;
import com.growthsee.common.utils.SecurityUtils;
import com.growthsee.common.utils.StringUtils;
import com.growthsee.common.utils.uuid.IdUtils;
import com.growthsee.rider.domain.RiderInfo;
import com.growthsee.rider.enumeration.VerificationTypeEnum;
import com.growthsee.rider.param.SmsParams;
import com.growthsee.rider.service.RiderInfoService;
import com.growthsee.rider.utils.QRCodeUtil;
import com.growthsee.rider.utils.SHA256Utils;
import com.growthsee.rider.utils.TxCloudSmsUtil;
import com.growthsee.rider.utils.ci.QCloudCosUtils;
import com.growthsee.rider.wrapper.CommonWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@AllArgsConstructor
@RestController
@RequestMapping("rider/common")
@Api("公共接口")
public class RiderCommonController {

    private final RiderInfoService riderInfoService;

    private final RedisCache redisCache;

    @ApiOperation(value = "sha256加密生成二维码", notes = "sha256加密生成二维码")
    @GetMapping("/qrcode")
    public void getQRCode(HttpServletResponse response, @RequestParam Map map){
        try {
            Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
            RiderInfo riderInfo =riderInfoService.getOne(Wrappers.<RiderInfo>lambdaQuery().eq(RiderInfo::getUserId,userId));
            String riderId = SHA256Utils.sha256(riderInfo.getId());
            QRCodeUtil.createCodeToOutputStream(riderId, response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @ApiOperation(value = "生成验证码 type:{0:注册验证码，1：修改用户信息验证码}", notes = "生成验证码")
    @GetMapping("/verification/code/{mobilePhone}")
    public AjaxResult getVerificationCode(@PathVariable("mobilePhone") String mobilePhone, @RequestParam(required = false,defaultValue="1")String type){
        if(StringUtils.equals(type, String.valueOf(VerificationTypeEnum.registered.getValue()))){
            String verifyResult = CommonWrapper.builder().verifyPhone(mobilePhone);
            if(StringUtils.isNotEmpty(verifyResult))return AjaxResult.error(verifyResult);
        }
        Integer verifyCode =randomCode();
        SmsParams smsParams = new SmsParams(mobilePhone,verifyCode.toString());
        String uuid = IdUtils.simpleUUID();
        String verifyKey = Constants.VERIFICATION_CODE_KEY + uuid;
        redisCache.setCacheObject(verifyKey, verifyCode.toString(), Constants.SMS_VERIFICATION_CODE, TimeUnit.MINUTES);
        System.out.println(verifyCode);
        String str = TxCloudSmsUtil.sendSms(smsParams);
        return  AjaxResult.success(uuid);
    }

    @ApiOperation(value = "获取腾讯云临时密钥", notes = "获取腾讯云临时密钥")
    @GetMapping("/temporary/key")
    public String getTemporaryKey(){
        return  QCloudCosUtils.getTemporaryKey().toString();
    }





    private int randomCode() {
        return (int) ((Math.random() * 9 + 1) * 100000);
    }
}
